<template>
  <view class="container">
    <!-- 页面标题 -->
    <view class="header">
      <text class="title">新增学生信息</text>
    </view>


 

    <!-- 学生信息输入表单 -->
    <view class="form-container">
      <input type="text" placeholder="请输入学生名字" v-model="studentName" />
      <input type="text" placeholder="请输入家长电话" v-model="parentPhone" />
	  
	  
      <picker @change="schoolChange" :value="selectedSchoolIndex" :range="schools">
        <view class="picker">
			<view class="uni-input">{{schools[selectedSchoolIndex]}}</view>
        </view>
      </picker>
	  
	  <picker @change="grade_Change" :value="selected_grade_Index" :range="grade">
	    <view class="picker">
	      <view class="uni-input">{{grade[selected_grade_Index]}}</view>
	    </view>
	  </picker>
	  
	  <picker @change="class_Change" :value="selected_class_Index" :range="class_">
	    <view class="picker">
	      <view class="uni-input">{{class_[selected_class_Index]}}</view>
	    </view>
	  </picker>
	  
	  
	  <view class="joinbutton" @tap="addNewStudent">
	  	<view  class="join_btn" >立刻提交</view>
	  </view>
	  
    </view>

 

 
  </view>
</template>





<script>
	
	import * as school_api from '@/api/school'
	import * as UserApi from '@/api/user'
	
export default {
  data() {
    return {
          studentName: '',
          parentPhone: '',
          selectedSchoolIndex: 0,
          selected_grade_Index: 0,
          selected_class_Index: 0,
          schools: ['请选择学校'], // 示例学校列表，您可以根据实际情况修改
          grade: ['请选择年级'], // 示例学校列表，您可以根据实际情况修改
          class_: ['请选择班别'], // 示例学校列表，您可以根据实际情况修改
		  department_structure: []
        };
  },
  onLoad() {
	this.school_list();
	  
    // 页面加载时执行的操作
	this.selectedSchoolIndex = 0;
	this.selected_grade_Index = 0;
	this.selected_class_Index = 0;
  },
  onUnload() {
    // 页面卸载时执行的操作
  },
 
  methods: 
  {
	  
	  // 获取所有学校的结构
	  school_list() {
	  	school_api.list().then(res => {
			// 假设res.info是一个JSON格式的字符串
			
			this.department_structure = res.info
			
	  		
			 let school_list= res.info.map(item => item.name);
			 console.log(school_list,"school_list")
			 this.schools.push(...school_list);
			 
			 console.log(this.schools,"this.schools")
			 console.log(this.schools[2],"this.schools")
	  	})
	  },
	  
	  
	  schoolChange: function(e)  {
		  
		  //清空一下班级
		  this.selected_grade_Index = 0;
		  this.selected_class_Index = 0;
		  
		  
		  this.selectedSchoolIndex = e.target.value;
		  console.log('picker发送选择改变，携带key值为', e.detail.value)
		  this.selectedSchoolIndex = e.detail.value
		  
		  
		  let now_school = this.schools[this.selectedSchoolIndex]
		  console.log('当前选中',now_school);
		  let grade_list= [];
		  
		  //开始从结构中提出当前学校的年级
		  this.department_structure.forEach(function(item, index) {
		    if(item.name == now_school)
			{
				grade_list= item[now_school];
				console.log("当前年级",grade_list);
			}	
		  });
		  
	
		  let grade_name_list= grade_list.map(item => item.name);
		  this.grade.push(...grade_name_list);
		  
	      },
		  
		  
		  
		  
		  grade_Change: function(e)  {
			  console.log(e.target.value)
		
		this.selected_class_Index = 0;
		  this.selected_grade_Index = e.target.value
			  
		  
		  let now_school = this.schools[this.selectedSchoolIndex]
		  let now_grade = this.grade[this.selected_grade_Index]
		  console.log('now_school',now_school);
		  console.log('now_grade',now_grade);
		  let grade_list= [];
		  let class_list= [];
		  
		  
		  //开始从结构中提出当前学校的年级
		  this.department_structure.forEach(function(item, index) {
		    if(item.name == now_school)
		  			{
		  				grade_list= item[now_school];
		  				console.log("当前年级",grade_list);
						grade_list.forEach(function(item, index) {
							if(item.name == now_grade)
								{
									class_list= item[now_grade];
									console.log("当前班别",class_list);
								}	
						});
		  			}
		  			
		  });
		  
		  	
			let class_name_list= class_list.map(item => item.name);
			this.class_.push(...class_name_list);
			
		   // this.class_= class_list.map(item => item.name);
		  
		  
		  
	      },
		  
 
		  class_Change(e) {
			  console.log(e.target.value);
			  this.selected_class_Index = e.target.value;
		  },
		  
		  
		  
	      addNewStudent() {
 
	        const newStudent = {
			  member_nid:uni.getStorageSync('user_id'),
	          name: this.studentName,
	          phone: this.parentPhone,
	          school: this.schools[this.selectedSchoolIndex],
	          grade: this.grade[this.selected_grade_Index],
	          class: this.class_[this.selected_class_Index],
	        };
	  
	        // 这里可以将新学生信息提交到后端或存储到本地数据中
	        console.log('新增学生:', newStudent);
	  
			//检查newStudent是否为空
			if(newStudent.name == '' || newStudent.phone == '' || newStudent.school == '' || newStudent.grade == '' || newStudent.class == '' || newStudent.school == '请选择学校' || newStudent.grade == '请选择年级' || newStudent.class == '请选择班级')
			{
				uni.showToast({
				    title: '请填写完整信息',
				    icon: 'none'
				});
				return;
			}
			
	 
		 
			//检查年级是否合法
			var reg = /^[\u4e00-\u9fa5]{2,4}$/;
			if (!reg.test(newStudent.grade)) {
			    uni.showToast({		
			        title: '年级格式不正确',
			        icon: 'none'
			    });
			    return;
			}
			
			//检查班级是否合法
			var reg = /^[\u4e00-\u9fa5a-zA-Z0-9]{2,20}$/;  
			if (!reg.test(newStudent.class)) {
			    uni.showToast({
			        title: '班级格式不正确',
			        icon: 'none'
			    });
			    return;
			}
			
			//检查学校是否合法
			var reg = /^[\u4e00-\u9fa5]{2,50}$/;
			if (!reg.test(newStudent.school)) {
			    uni.showToast({
			        title: '学校格式不正确',
			        icon: 'none'
			    });
			    return;
			}
			
			
			//调用add接口，提交当前的数据
			UserApi.children_add(newStudent).then(res => {
				
				//补充根据返回信息判断问题
				if(res.code == 200)
				{
				
				uni.showToast({
				    title: '添加成功',
				    icon: 'success'
				});
				// 清空输入框
				this.studentName = '';
				this.parentPhone = '';
				this.selectedSchoolIndex = 0;
				this.selected_grade_Index = 0;
				this.selected_class_Index = 0;
				
				uni.navigateTo({
					url:"/pages/user/student_list"
				})
				
				
				}
			}).catch(err => {
				uni.showToast({
				    title: '添加失败',
				    icon: 'none'
				});
			});
 
	      }
  }
};
</script>



<style scoped lang="scss">
	@import url("asset/style.css");
</style>
 